import {MainData, PK, pkType} from "../../coreModule/data/BaseData";
import {DataOccasion, field, occasion} from "../../coreModule/data/DataLoader";
import {DateUtils} from "../../../utils/DateUtils";
import {MathUtils} from "../../../utils/MathUtils";

export enum MessageStatus{
	Unread = -1, Read, Deleted
}

export const SystemSenderId = "system";

export class Message extends MainData {

	@field(pkType())
	public senderId: PK = SystemSenderId;
	@field(pkType())
	public receiverId: PK = "";
	@field(pkType())
	public noticeId?: PK; // 如果与指定的notice关联
	@field
	public title: string = "";
	@field
	public content: string = "";
	@field(Number)
	public createdAt: number;
	@field
	public status: MessageStatus = MessageStatus.Unread;

	// region 额外数据

	@field(String)
	@occasion(DataOccasion.Extra)
	createTimeStr: string;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	isRead: boolean;

	public refresh() {
		const isToday = DateUtils.isToday(this.createdAt);
		this.createTimeStr = isToday ?
			DateUtils.time2TimeStr(this.createdAt) :
			DateUtils.time2DateStr(this.createdAt);
		this.isRead = this.status == MessageStatus.Read;
	}

	// endregion

	public static testData(cnt = 8) {
		const res = [];
		for (let i = 0; i < cnt; i++) {
			const data = new Message();
			data._id = MathUtils.randomString();
			data.title = `测试消息${i}`;
			data.content = "很长很长的内容，很长很长很长很长很长很长很长很长很长很长很长很长的内容\n试试能不能换行？\n哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊";
			data.createdAt = Date.now() - Math.random() * 100000000 * i;
			data.status = Math.floor(Math.random() * 2) - 1;
			res.push(data);
		}
		return res;
	}

}
